<?php
namespace Home\Model;
use Think\Model;
class TempDiyModel extends Model
{
    public static $sp_id;

	// 解码首页动态设置
	public function decode_html( $html_conf )
    {
        self::$sp_id = session("url_shop_id");
        $html = "";
		foreach($html_conf as $html_k => $html_v){
            $html_conf = unserialize($html_v["temp_conf"]);
            switch($html_v["temp_code"]){
                case 'banner': $html .= self::get_banner_html( $html_conf );break;
                case 'ad': $html .= self::get_ad_html( $html_conf );break;
                case 'diy': $html .= self::get_diy_html( $html_conf );break;
                case 'nav': $html .= self::get_nav_html( $html_conf );break;
                case 'blank': $html .= self::get_blank_html( $html_conf );break;
                case 'goods': $html .= self::get_goods_html( $html_conf );break;
                case 'cats': $html .= self::get_cats_html( $html_conf );break;
                case 'notice': $html .= self::get_nts_html( $html_conf );break;
                case 'search': $html .= self::get_search_html();break;
            }
        }
        return $html;
	}

    static function get_banner_html( $conf )
    {
        $html = "<div class='swiper-container'><div class='swiper-wrapper'>";
        foreach($conf as $conf_k => $conf_v){
            $html .= "<div class='swiper-slide'><a href='{$conf_v["linkurl"]}'><img src='{$conf_v["img"]}'/></a></div>";
        }
        $html .= "</div><div class='swiper-pagination'></div></div>";
        return $html;
    }

    static function get_nts_html( $conf )
    {
        $html = "<div class='advert'>
        <div class='advert-notice'>
            <div class='advert-pic'><span class='advert-icon'></span></div>
            <span class='notice-p'>公告：</span>
            <div class='txtMarquee-top'><div class='bd'><ul class='infoList'>";

        foreach($conf as $t_k => $t_v){
            $html .= "<li><a href='{$t_v["linkurl"]}' target='_blank'>{$t_v["title"]}</a></li>";
        }
        $html .= "</ul></div></div></div></div>";
        return $html;
    }

    static function get_search_html()
    {
        $html = "<div class='goods-search'><form action='' method='get'>
            <input type='text' placeholder='搜索商品' class='search-ipt' name='mName' required/>
            <input type='hidden' name='m' value='Home'/>
            <input type='hidden' name='c' value='Good'/>
            <input type='hidden' name='a' value='agentgoods'/>
            <input type='submit' class='submit-ipt' value=''/></form></div>";
        return $html;
    }

    static function get_goods_html( $conf )
    {
        if(isset($conf['goods_ids'])){
            $where = "id IN ({$conf['goods_ids']}) AND shop_id = ". self::$sp_id;
            $limit = "0,100";
        }elseif(isset($conf['cats_ids'])){
            $where = "shelves = 1 AND cate IN ({$conf['cats_ids']}) AND shop_id = ". self::$sp_id;
            $limit = $conf["style"] == 1 ? "0,9" : "0,6";
        }else{
            return "";
        }
        $goods = M("good")->where($where)->field("id,img,g_name,price,stock")->limit($limit)->order("sale_num desc")->select();
        if($conf["style"] == 1){
            $html = "<ul class='fruit-ul'>";
            foreach($goods as $g_k => $g_v){
                $html .= "<li><a href='".U("Good/goodsdetail",array("id" => $g_v["id"]))."'>
                    <img src='{$g_v["img"]}'/>
                    <span class='fruit-name'>{$g_v["g_name"]}</span>
                    <span class='fruit-price'>￥{$g_v["price"]}</span>
                </a>
            </li>";
            }
            $html .= "</ul>";
        }else{
            $html = "<ul class='choise-top'>";
            foreach($goods as $g_k => $g_v){
                $html .= "<li>
                    <div class='choise-pic'><a href='".U("Good/goodsdetail",array("id" => $g_v["id"]))."'><img src='{$g_v["img"]}'/></a></div>
                    <div class='choise-msg'>
                        <span class='choise-name'>{$g_v["g_name"]}</span>
                        <span class='choise-price'>￥{$g_v["price"]}</span>
                        <a href='javascript:;' class='choise-shopcar cart' data-id='{$g_v["id"]}'></a>
                    </div>
            </li>";
            }
            $html .= "</ul>";
        }
        return $html;
    }

    static function get_ad_html( $conf )
    {
        $html = "<div class='advert-img'>
                    <a href='{$conf["linkurl"]}'><img src='{$conf["img"]}'/></a>
                </div>";
        return $html;
    }

    static function get_cats_html( $conf )
    {
        $html = "<div class='goods-kinds'><ul class='kinds-ul'>";
        foreach($conf as $c_k => $c_v){
            $html .= "<li><a href='{$c_v["linkurl"]}'><img src='{$c_v["img"]}'/><span>{$c_v["cname"]}</span></a></li>";
        }
        $html .= "</ul></div>";
        return $html;
    }

    static function get_blank_html( $conf )
    {
        $html = "<p style='background:transparent;margin:0;padding:0;height:{$conf["height"]}px;'></p>";
        return $html;
    }

    static function get_nav_html( $conf )
    {
        $html = "<div class='index-top'>
                    <span class='index-top-name'>{$conf["title"]}</span>
                    <a href='{$conf["linkurl"]}' class='index-top-more'>更多</a>
                </div>";
        return $html;
    }

    static function get_diy_html( $conf )
    {
        if($conf[0]["module"] == 1){
            $html = "<div class='advert-img'>
                        <a href='{$conf[0]["linkurl"]}'><img src='{$conf[0]["img"]}'/></a>
                    </div>";
        }
        if($conf[0]["module"] == 2){
            $html = "<div class='hot-pic'>
                        <div class='hot-pic-img'>
                            <a href='{$conf[0]["linkurl"]}'><img src='{$conf[0]["img"]}'/></a>
                        </div>
                        <div class='hot-pic-img'>
                            <a href='{$conf[1]["linkurl"]}'><img src='{$conf[1]["img"]}'/></a>
                            <a href='{$conf[2]["linkurl"]}'><img src='{$conf[2]["img"]}'/></a>
                        </div>
                    </div>";
        }
        if($conf[0]["module"] == 3){
            $html = "<div class='hot-pic'>
                        <div class='hot-pic-img'>
                            <a href='{$conf[0]["linkurl"]}'><img src='{$conf[0]["img"]}'/></a>
                        </div>
                        <div class='hot-pic-img'>
                            <a href='{$conf[1]["linkurl"]}'><img src='{$conf[1]["img"]}'/></a>
                            <a href='{$conf[2]["linkurl"]}'><img src='{$conf[2]["img"]}'/></a>
                        </div>
                    </div>";
        }
        if($conf[0]["module"] == 4){
            $html = "<div class='hot-pic'>
                        <div class='hot-pic-img'>
                            <a href='{$conf[0]["linkurl"]}'><img src='{$conf[0]["img"]}'/></a>
                        </div>
                        <div class='hot-pic-img'>
                            <a href='{$conf[1]["linkurl"]}'><img src='{$conf[1]["img"]}'/></a>
                            <a href='{$conf[2]["linkurl"]}'><img src='{$conf[2]["img"]}'/></a>
                        </div>
                    </div>";
        }
        return $html;
    }
}